<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 24/01/2019
 * Time: 12:02
 */

namespace App\Modules\UCenter;


use App\Services\UCenter\UCenterIndex;
use Illuminate\Http\Request;


class WayBill extends BaseUcenter
{
    /**
     * 车主接单形成运单
     * @return array
     */
    public function createPrivateWayBill()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        $data['phone'] = $userInfo['res']['phone'];
        return UCenterIndex::WayBill()->createPrivateWayBill($data);
    }

    /**
     * 公司接单形成运单
     * @return array
     */
    public function createCompanyWayBill()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->createCompanyWayBill($data);
    }

    /**
     * 根据id获取运单详情
     * @return array
     */
    public function getWayBillDetail()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        $data['role_ids'] = $userInfo['res']['role_ids'];
        return UCenterIndex::WayBill()->getWayBillDetail($data);
    }

    /**
     * 根据user_id获取公司下车辆与司机
     * @return array
     */
    public function getCompanyCarAndDriver(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->getCompanyCarAndDriver($data);
    }

    /**
     * 添加订阅路线
     */
    public function addSubscription()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->addSubscription($data);
    }

    /**
     * 获取订阅路线列表
     * @return array
     */
    public function getSubList()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->getSubList($data);
    }

    /**
     * 根据id删除订阅路线
     * @return array
     */
    public function delSubById()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;

        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->delSubById($data);
    }

    /**
     * 根据user_id获取个人信息与车辆
     * @return array
     */
    public function getPrivateAndCar(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->getPrivateAndCar($data);
    }

    /**
     * 后台运单列表
     * @return array
     */
    public function waybillList(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->waybillList($data);
    }

    /**
     * 货主运单列表
     * @return array
     */
    public function wayBillOwnerList(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->wayBillOwnerList($data);
    }

    /**
     * 添加委托信息
     * @return array
     */
    public function createEntrust(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->createEntrust($data);
    }

    /**
     * 取消订单
     * @return array
     */
    public function cancelWayBill(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->cancelWayBill($data);
    }

    /**
     * 车主运单
     * @return array
     */
    public function companyWayBillList(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->companyWayBillList($data);
    }

    /**
     * 取消委托
     * @return array
     */
    public function cancelEntrust(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->cancelEntrust($data);
    }

    /**
     * 委托详情
     * @return array
     */
    public function entrustDetail(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->entrustDetail($data);
    }

    /**
     * 修改委托
     * @return array
     */
    public function updateEntrust(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->updateEntrust($data);
    }

    /**
     * 委托列表
     * @return array
     */
    public function entrustList(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->entrustList($data);
    }

    /**
     * 修改运单
     * @return array
     */
    public function updateWayBill(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->updateWayBill($data);
    }

    /**
     * 举报投诉
     * @return array
     */
    public function createComplain(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->createComplain($data);
    }

    /**
     * 举报详情
     * @return array
     */
    public function complainDetail(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->complainDetail($data);
    }

    /**
     * 用户是否同意投诉处理结果
     * @return array
     */
    public function complainDeal(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->complainDeal($data);
    }

    /**
     * 确定装车
     * @return array
     */
    public function truckLoading(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['role_ids'] = $userInfo['res']['role_ids'];
        return UCenterIndex::WayBill()->truckLoading($data);
    }

    /**
     * 完成卸货
     * @return array
     */
    public function truckUnload(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->truckUnload($data);
    }

    /**
     * 延时付款
     * @return array
     */
    public function delayPay()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo, '请登录');
        }
        $data = $this->postData;
        $time = strtotime("+1 day");
        \App\Models\Cargo\WayBill::where('id','=',$data['id'])->update(['pay_time'=>$time]);
        $swoole = new \swoole_client(SWOOLE_SOCK_TCP);
        $swoole->connect('127.0.0.1', 9501, 1);
        $msg = $data['id'];
        $swoole->send($msg);
    }

    /*
     * 运单支付
     */
    public function pay()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->pay($data);
    }

    /*
     * 确认到达
     */
    public function arrive()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['role_ids'] = $userInfo['res']['role_ids'];
        return UCenterIndex::WayBill()->arrive($data);
    }

    /**
     * 车主支付保证金
     */
    public function payDeposit()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->payDeposit($data);
    }
    /**
     * 后台获取运单详情
     * @return array
     */
    public function waybillInfo()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->waybillInfo($data);
    }

    /**
     * 委托统计
     * @return array
     */
    public function entrustStatistics()
    {
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->entrustStatistics($data);
    }

    //根据车牌号查找对应的司机
    public function companyByNumber(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->companyByNumber($data);
    }

    //委托拒绝接单
    public function entrustRefuse(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        return UCenterIndex::WayBill()->entrustRefuse($data);
    }

    //通话记录
    public function createRecord(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->createRecord($data);
    }

    //记录列表
    public function recordList(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->recordList($data);
    }

    //清空记录
    public function deleteRecord(){
        $userInfo = $this->isLogin();
        if ($userInfo['status'] !== SUCCESS_STATUS) {
            return need_login($userInfo,'请登录');
        }
        $data = $this->postData;
        $data['user_id'] = $userInfo['res']['id'];
        return UCenterIndex::WayBill()->deleteRecord($data);
    }

}
